$(function () {
    //浏览器调试
    let JIM = new JMessage();
    let markId = getUrlString('markid');
    let username;
    let password;
    let appkey,random_str,signature,timestamp;

    //添加好友
    $('#goAddFrd').click(function () {
        window.location.href = 'dancool://web=https://api.odancool.com/weixinapp/view/messages/searchUser.html?markid='+markId
    });

    //验证消息
    $('#verifyMsgImg').click(function () {
        window.location.href = 'dancool://web=https://api.odancool.com/weixinapp/view/messages/verifyMsg.html?markid='+markId
    });

    //群组
    $('#goGroupList').click(function () {
        window.location.href = 'dancool://web=https://api.odancool.com/weixinapp/view/messages/groupList.html?markid='+markId
    });

    //黑名单
    $('#goBlackList').click(function () {
        window.location.href = 'dancool://blacklist='+markId
    });

    //获取appid
    function getAppid() {
        layLoading();
        getData('post','/niu/apiv3/member_jmauth.php',{'markid':markId},function (data) {
            console.log(data);
            username = data.imid;
            password = data.impw;
            appkey = data.appkey;
            random_str = data.random_str;
            signature = data.signature;
            timestamp = data.timestamp;
            //调用初始化
            init();
        })
    }
    getAppid();

    //初始化
    function init() {
        JIM.init({
            "appkey" : appkey,
            "random_str" : random_str,
            "signature" : signature,
            "timestamp" : timestamp,
            "flag" : 0
        }).onSuccess(function(data) {
            console.log(data);
            login(username,password)
            //data.code 返回码
            //data.message 描述
        }).onFail(function(data) {
            // 同上
            codeError(data.code)
        });
    }

    //登录
    function login(username,password) {

        JIM.login({
            'username' : username,
            'password': password
        }).onSuccess(function(data) {
            getFriendList();
        }).onFail(function(data) {
            codeError(data.code)
        }).onTimeout(function(data) {
            console.log('timeout:' + JSON.stringify(data));
        });

        JIM.onEventNotification(function(data) {
            console.log(data);
            if(data.event_type === 5){
                var obj = document.getElementById("verifyMsgImg");
                var timer = null;
                var i = 0;
                timer=setInterval(function(){
                    obj.style.opacity=i++%2?"0":"1";
                },800);
                $('#verifyMsg').click(function () {
                    clearInterval(timer);
                })
            }
        });
    }



    function getFriendList(){
        JIM.getFriendList().onSuccess(function(data) {
            layer.closeAll('loading');
            console.log(data);
            let append = '';
            if (data.friend_list.length === 0){
                $('#noFrd').show()
            } else {
                $(data.friend_list).each(function () {
                    append += ' <li>' +
                        '            <div class="left">' +
                        '                <img src="../../res/img/eggTV/1.png" alt="">' +
                        '            </div>' +
                        '            <div class="right">' +
                        '                <span>爱蜜莉雅</span>' +
                        '            </div>' +
                        '        </li>'
                });
                $('#conversationList').append(append)
            }
        }).onFail(function(data) {
            codeError(data.code)
        });
    }

    //获取用户的个人信息
    function getSelfInfo(username,isMe) {
        JIM.getUserInfo({
            'username' : username
        }).onSuccess(function(data) {
            console.log(data);
            getResource(data.user_info.avatar).then(function(successData) {

            })
        }).onFail(function(data) {
            console.log(data);
            codeError(data.code)
        });

    }

    //获取图片资源
    function getResource (media_id) {
        return new Promise(function(success,fail) {
            JIM.getResource({
                'media_id' : media_id,
            }).onSuccess(function(data) {
                // console.log(data);
                success(data.url)
                //data.code 返回码
                //data.message 描述
                //data.url 资源临时访问路径，具体超时时间expire time会包含在url中
            }).onFail(function(data) {
                codeError(data.code)
                //data.code 返回码
                //data.message 描述
            });
        });
    }
});